package com.fstech.whiteboadrd.pop;


import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fstech.whiteboadrd.R;
import com.fstech.whiteboadrd.control.ControlManager;
import com.fstech.whiteboadrd.data.DataManager;
import com.fstech.whiteboadrd.lib.Utils.ScreenUtils;
import com.fstech.whiteboadrd.lib.bean.SketchData;
import com.fstech.whiteboadrd.utils.UIUtils;
import com.fstech.whiteboadrd.widget.MyRadioGroup;

import java.util.List;
import java.util.zip.Inflater;

public class PopManager implements View.OnClickListener {

    private PopupWindow mPopupWindow;

    View menuView;
    View eraseView;
    View penView;
    View clearView;
    View bgView;
    View pageView;

    Context mContext;
    private ControlManager.ControlCallback mCallback;
    private BaseQuickAdapter<SketchData, BaseViewHolder> mAdapter;

    public void setCallback(ControlManager.ControlCallback callback) {
        mCallback = callback;
    }

    @Override
    public void onClick(View v) {
        mPopupWindow.dismiss();
        switch (v.getId()) {
            case R.id.pop_clearcancel:
                break;
            case R.id.pop_clearsure:
                if (mCallback != null) {
                    mCallback.onClear(true, null);
                }
                break;
            case R.id.menu_insert:
                if (mCallback != null) {
                    mCallback.onInsert();
                }
                break;
            case R.id.menu_open:
                if (mCallback != null) {
                    mCallback.onOpen();
                }
                break;
            case R.id.menu_quit:
                if (mCallback != null) {
                    mCallback.onquit();
                }
                break;
            case R.id.menu_save:
                if (mCallback != null) {
                    mCallback.onSave(null);
                }
                break;
            case R.id.menu_share:
                if (mCallback != null) {
                    mCallback.onShare(1);
                }
                break;
            case R.id.menu_share_email:
                if (mCallback != null) {
                    mCallback.onShare(2);
                }
                break;
            case R.id.menu_chart:
                if (mCallback != null) {
                    mCallback.onInsertChart();
                }
                break;
        }
    }

    public enum POPWINDOW_TYPE {
        POP_MENU, POP_PEN, POP_ERASE, POP_CLEAR, POP_BG, POP_PAGE
    }

    private PopManager(Context context) {
        mContext = context;
        initPop();
    }

    private void initPop() {
        mPopupWindow = new PopupWindow(mContext);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());//设置空白背景
        mPopupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);//动画
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });
    }

    private static PopManager mInstance;

    public static PopManager getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new PopManager(context);
        }
        return mInstance;
    }

    public void show(POPWINDOW_TYPE type, View anchorView) {
        if (mPopupWindow.isShowing()) {
            try {
                mPopupWindow.dismiss();
            } catch (Exception e) {
                initPop();
            }
        }
        switch (type) {
            case POP_MENU:
                if (menuView == null) {
                    menuView = inflate(R.layout.pop_menu);
                    initMenuView(menuView);
                }
                mPopupWindow.setContentView(menuView);
                menuView.measure(0, 0);
                Log.e("yangxing", "show:" + menuView.getMeasuredHeight());
                mPopupWindow.setWidth(menuView.getMeasuredWidth());//宽度
                mPopupWindow.setHeight(menuView.getMeasuredHeight());//高度自适应
                mPopupWindow.showAsDropDown(anchorView, -getDimension(R.dimen.x12), getDimension(R.dimen.x6));
                break;
            case POP_PEN:
                if (penView == null) {
                    penView = inflate(R.layout.pop_pen);
                    initPenView(penView);
                }
                mPopupWindow.setContentView(penView);
                penView.measure(0, 0);
                mPopupWindow.setWidth(UIUtils.getDimens(R.dimen.x160));//宽度
                mPopupWindow.setHeight(UIUtils.getDimens(R.dimen.y110));//高度自适应
                mPopupWindow.showAsDropDown(anchorView, 0, getDimension(R.dimen.x6));
                break;
            case POP_ERASE:
                if (eraseView == null) {
                    eraseView = inflate(R.layout.pop_erase);
                    initEraseView(eraseView);
                }
                mPopupWindow.setContentView(eraseView);
                eraseView.measure(0, 0);
                mPopupWindow.setWidth(eraseView.getMeasuredWidth());//宽度
                mPopupWindow.setHeight(eraseView.getMeasuredHeight());//高度自适应
                mPopupWindow.showAsDropDown(anchorView, 0, getDimension(R.dimen.x6));
                break;
            case POP_CLEAR:
                if (clearView == null) {
                    clearView = inflate(R.layout.pop_clear);
                    initClearView(clearView);
                }
                mPopupWindow.setContentView(clearView);
//                mPopupWindow.setWidth(ScreenUtils.dip2px(mContext, ,));//宽度
                mPopupWindow.setWidth(getDimension(R.dimen.x175));//宽度
//                mPopupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);//宽度
                mPopupWindow.setHeight(getDimension(R.dimen.y96));//高度自适应
                mPopupWindow.showAsDropDown(anchorView, 0, getDimension(R.dimen.x6));
                break;
            case POP_BG:
                if (bgView == null) {
                    bgView = inflate(R.layout.pop_bg);
                    initBgView(bgView);
                }
                mPopupWindow.setContentView(bgView);
                mPopupWindow.setWidth(getDimension(R.dimen.x182));//宽度
                mPopupWindow.setHeight(getDimension(R.dimen.y188));//高度自适应
                mPopupWindow.showAsDropDown(anchorView, 0, getDimension(R.dimen.x6) * 2);
                break;
            case POP_PAGE:
                if (pageView == null || mAdapter == null) {
                    pageView = inflate(R.layout.pop_pageswich);
                }
                setPageView(pageView);
                mPopupWindow.setContentView(pageView);
                mPopupWindow.setWidth(getDimension(R.dimen.x186));//宽度
                mPopupWindow.setHeight(getDimension(R.dimen.y358));//高度自适应
                mPopupWindow.showAsDropDown(anchorView, getDimension(R.dimen.x20), getDimension(R.dimen.x10));
                break;
            default:
                break;
        }
    }

    private void setPageView(View pageView) {
        final RecyclerView pop_pages = pageView.findViewById(R.id.pop_pages);
        if (mAdapter == null) {
            mAdapter = new BaseQuickAdapter<SketchData, BaseViewHolder>(R.layout.item_page, DataManager.getInstance().getDataList()) {
                @Override
                protected void convert(BaseViewHolder helper, SketchData item) {
                    TextView page_title = helper.getView(R.id.page_title);
                    ImageView page_close = helper.getView(R.id.page_close);
                    ImageView page_image = helper.getView(R.id.page_image);
                    final int position = DataManager.getInstance().getIndex(item);
                    page_title.setText(UIUtils.getString(R.string.canvas) + " " + (position + 1));
                    page_close.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mCallback != null) {
                                mCallback.onPageDel(position);
                            }
                            notifyDataSetChanged();
                        }
                    });
                    page_image.setImageBitmap(item.thumbnailBM);
                    boolean isSelected = (position == (DataManager.getInstance().getCurrentIndex() - 1));
                    helper.getView(R.id.pageview).setSelected(isSelected);
                }
            };
            mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    if (mCallback != null) {
                        mCallback.onPageSel(position);
                    }
//                    view.setSelected(true);
                    mAdapter.notifyDataSetChanged();
                }
            });
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            pop_pages.setLayoutManager(linearLayoutManager);
            pop_pages.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
        pop_pages.scrollToPosition(DataManager.getInstance().getCurrentIndex() - 1);
    }

    private void initBgView(View bgView) {
        MyRadioGroup pen_colors = bgView.findViewById(R.id.pen_colors);
        pen_colors.setOnCheckedChangeListener(new MyRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(MyRadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.bg_yellow:
                        DataManager.getInstance().setBgColorRes(R.color.bg_yello);
                        break;
                    case R.id.bg_blue:
                        DataManager.getInstance().setBgColorRes(R.color.bg_blue);
                        break;
                    case R.id.bg_gray:
                        DataManager.getInstance().setBgColorRes(R.color.bg_gray);
                        break;
                    case R.id.bg_black:
                        DataManager.getInstance().setBgColorRes(R.color.bg_black);
                        break;
                    case R.id.bg_cyan:
                        DataManager.getInstance().setBgColorRes(R.color.bg_cyan);
                        break;
                    case R.id.bg_white:
                        DataManager.getInstance().setBgColorRes(R.color.bg_white);
                        break;
                }
                if (mCallback != null) {
                    mCallback.onBg(true, null);
                }
            }
        });
    }

    private void initPenView(View penView) {
        final DataManager instance = DataManager.getInstance();
        MyRadioGroup pen_colors = penView.findViewById(R.id.pen_colors);
        RadioGroup pen_sizes = penView.findViewById(R.id.pen_sizes);
        pen_sizes.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.pen_size1:
                        instance.setPenSize(DataManager.PEN_SIZE_SMALL);
                        break;
                    case R.id.pen_size2:
                        instance.setPenSize(DataManager.PEN_SIZE_MEDIUM);
                        break;
                    case R.id.pen_size3:
                        instance.setPenSize(DataManager.PEN_SIZE_BIG);
                        break;
                }
                refreshPen();
            }
        });

        pen_colors.setOnCheckedChangeListener(new MyRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(MyRadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.pen_red:
                        instance.setPenColorRes(R.color.red);
                        break;
                    case R.id.pen_yellow:
                        instance.setPenColorRes(R.color.yellow);
                        break;
                    case R.id.pen_blue:
                        instance.setPenColorRes(R.color.blue_pen);
                        break;
                    case R.id.pen_green:
                        instance.setPenColorRes(R.color.green);
                        break;
                    case R.id.pen_gray1:
                        instance.setPenColorRes(R.color.gray1_pen);
                        break;
                    case R.id.pen_bluewater:
                        instance.setPenColorRes(R.color.blue_water);
                        break;
                    case R.id.pen_black:
                        instance.setPenColorRes(R.color.black);
                        break;
                    case R.id.pen_white:
                        instance.setPenColorRes(R.color.white_pen);
                        break;
                    case R.id.pen_pink:
                        instance.setPenColorRes(R.color.pink);
                        break;
                }
                refreshPen();
            }
        });
    }

    private void refreshPen() {
        if (mCallback != null) {
            mCallback.onPen(true, null);
        }
    }

    private void initEraseView(View eraseView) {
        RadioGroup erase_sizes = eraseView.findViewById(R.id.erase_sizes);
        erase_sizes.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.erase_size1:
                        DataManager.getInstance().setEraseSize(DataManager.ERASE_SIZE_SMALL);
                        break;
                    case R.id.erase_size2:
                        DataManager.getInstance().setEraseSize(DataManager.ERASE_SIZE_MEDIUM);
                        break;
                    case R.id.erase_size3:
                        DataManager.getInstance().setEraseSize(DataManager.ERASE_SIZE_BIG);
                        break;
                }
                if (mCallback != null) {
                    mCallback.onErase(true, null);
                }
            }
        });
    }

    private void initMenuView(View menuView) {
        menuView.findViewById(R.id.menu_open).setOnClickListener(this);
        menuView.findViewById(R.id.menu_save).setOnClickListener(this);
        menuView.findViewById(R.id.menu_share).setOnClickListener(this);
        menuView.findViewById(R.id.menu_insert).setOnClickListener(this);
        menuView.findViewById(R.id.menu_quit).setOnClickListener(this);
        menuView.findViewById(R.id.menu_share_email).setOnClickListener(this);
        menuView.findViewById(R.id.menu_chart).setOnClickListener(this);
    }

    private int getDimension(int resId) {
        return (int) mContext.getResources().getDimension(resId);
    }

    private void initClearView(View rootView) {
        rootView.findViewById(R.id.pop_clearsure).setOnClickListener(this);
        rootView.findViewById(R.id.pop_clearcancel).setOnClickListener(this);
    }

    private View inflate(int layoutId) {
        return View.inflate(mContext, layoutId, null);
    }

}
